package com.hhw.rtmptester;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private final String url = "rtmp://192.168.2.104:1935/live/home";
    private MediaProjectionManager mediaProjectionManager;
    private MediaProjection mediaProjection;
    private ScreenLive mScreenLive;

    private final int REQUEST_CODE_SCREEN_CAPTURE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //请求开始录屏
        this.mediaProjectionManager = (MediaProjectionManager)getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        Intent captureIntent = mediaProjectionManager.createScreenCaptureIntent();
        startActivityForResult(captureIntent, REQUEST_CODE_SCREEN_CAPTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE_SCREEN_CAPTURE && resultCode == Activity.RESULT_OK) {
            //如果用户同意开始采集屏幕
            mediaProjection = mediaProjectionManager.getMediaProjection
                    (resultCode, data);
            mScreenLive = new ScreenLive();
            mScreenLive.startLive(url, mediaProjection);
        }
    }

    /**
     * 停止录屏
     * @param view
     */
    public void stopLive(View view) {
        if (mediaProjection != null){
            this.mediaProjection.stop();
        }
    }
}